package com.et.machine.manager.vm.goods;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.et.machine.R;
import com.et.machine.databinding.DialogAddGoodsCountBinding;
import com.et.machine.store.ChannelEntity;

public class AddGoodsCountDialog extends Dialog {
    private final OnDialogClickListener customDialogListener;
    private DialogAddGoodsCountBinding binding;
    private final ChannelEntity channelEntity;


    public AddGoodsCountDialog(Context context, ChannelEntity entity, OnDialogClickListener customDialogListener) {
        super(context, R.style.common_dialog);
        this.customDialogListener = customDialogListener;
        this.channelEntity = entity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogAddGoodsCountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.channel.setText(String.valueOf(channelEntity.getChannel()));
        binding.number.setText(String.valueOf(channelEntity.getQuantity()));
        binding.number.setSelection(0, binding.number.getText().length());
        binding.ensure.setOnClickListener(mEnsureClickListener);
        binding.cancel.setOnClickListener(mCancelClickListener);
    }

    //发送消息,回调函数取值;
    private final View.OnClickListener mEnsureClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            channelEntity.setQuantity(Integer.parseInt(binding.number.getText().toString()));
            customDialogListener.onClick(channelEntity);
            dismiss();
        }
    };

    //取消发送;
    private final View.OnClickListener mCancelClickListener = v -> {
        dismiss();  //关闭提示框
    };

    //定义回调事件，用于dialog的点击后的处理事件
    public interface OnDialogClickListener {
        void onClick(ChannelEntity entity);
    }

}
